package com.zjxh.zjxh.common;
;
import com.zjxh.zjxh.common.constant.CMEnum;
import com.zjxh.zjxh.common.exception.CommonException;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * @author: oyp
 * @date: 2022/3/8 on 14:39
 * @desc: 响应体
 */
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Data
public class Result<T> implements Serializable {

  private static final long serialVersionUID = 8367097146768059750L;

  @ApiModelProperty("返回码")
  private String code;

  @ApiModelProperty("提示信息")
  private String msg;

  @ApiModelProperty("data")
  private T obj;

  public static <T> Result<T> success(T obj) {
    return new Result<>(CMEnum.SUCCESS.getCode(), "", obj);
  }

  public static <T> Result<T> success(CMEnum cm) {
    return new Result<>(cm.getCode(), cm.getMsg(), null);
  }

  public static <T> Result<T> success(CMEnum cm, String msg) {
    return new Result<>(cm.getCode(), msg, null);
  }

  public static <T> Result<T> success() {
    return new Result<>(CMEnum.SUCCESS.getCode(), "", null);
  }

  public static <T> Result<T> error(CMEnum cmEnum) {
    return new Result<>(cmEnum.getCode(), cmEnum.getMsg(), null);
  }

  public static <T> Result<T> error(CMEnum cmEnum, String msg) {
    return new Result<>(cmEnum.getCode(), msg, null);
  }

  public static <T> Result<T> error(CommonException e) {
    return new Result<>(e.getCode(), e.getMsg(), null);
  }
}
